const { queryDBPromise } = require('./util/db')
/**
 * 添加用户
 */
const testAddUser = () => {
	const username = 'jack'
	const password = 123
	const age = 20

	// const sql = "INSERT INTO users (username,password,age)VALUES('root','123',19)"
	const sql = `INSERT INTO users (username,password,age)VALUES('${username}','${password}',${age})`
	console.log('sql ', sql)

	queryDBPromise(sql)
		.then(data => {
			console.log('data >> :', data)
			console.log('添加用户成功')
		})
		.catch(err => {
			console.log('添加用户出错', err)
		})
}

/**
 * 查看所有用户
 */
const testFindUserAll = () => {
	const sql = 'SELECT id,username,password,age FROM users'
	queryDBPromise(sql)
		.then(data => {
			console.log('data :', data) // 返回的查询用户数组
		})
		.catch(err => console.log(err))
}

/**
 * 修改用户
 * 根据用户id修改用户密码
 */
const testUpdateUser = () => {
	const id = 3
	const password = '123456'
	const sql = `UPDATE users SET password = '${password}' WHERE id = ${id}`
	queryDBPromise(sql)
		.then(data => {
			console.log('data :', data)
			console.log('修改用户成功')
		}) 
		.catch(err => {
			console.log('修改失败', err)
		})
}

/**
 * 删除用户
 * 根据用户id
 */
const testDeleteUser = () => {
	const id = 1
	const sql = `DELETE FROM users WHERE id = ${id}`
	queryDBPromise(sql)
		.then(data => {
			console.log('删除成功')
		})
		.catch(err => {
			console.log('删除失败', err)
		})
}

testDeleteUser()
